package com.eastfair.projectcore.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.projectcore.dto.ConfigAttributeDTO;
import com.eastfair.projectcore.dto.ConfigAttributePageQuery;
import com.eastfair.projectcore.dto.ConfigAttributeSaveDTO;
import com.eastfair.projectcore.dto.ConfigAttributeUpdateDTO;
import com.eastfair.projectcore.entity.ConfigAttribute;
import com.eastfair.projectcore.service.ConfigAttributeService;
import com.eastfair.projectcore.vo.ConfigAttributeVO;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.core.base.R;
import com.eastfair.echo.core.EchoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 配置模块属性表
 * </p>
 *
 * @author zjn
 * @date 2021-08-05
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/configAttribute")
@Api(value = "ConfigAttribute", tags = "配置模块属性表")
public class ConfigAttributeController extends SuperController<ConfigAttributeService, Long, ConfigAttribute, ConfigAttributePageQuery, ConfigAttributeSaveDTO, ConfigAttributeUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private ConfigAttributeService configAttributeService;

    @Override
    public void handlerResult(IPage<ConfigAttribute> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ConfigAttribute> configAttributeList = list.stream().map((map) -> {
            ConfigAttribute configAttribute = ConfigAttribute.builder().build();
            //TODO 请在这里完成转换
            return configAttribute;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(configAttributeList));
    }

    /**
     * 查询某配置类的属性
     *
     * @param configAttributeDTO 配置类属性对象
     * @return
     */
    @ApiOperation(value = "查询某配置类的属性", notes = "查询某配置类的属性")
    @PostMapping("/listByEntityClass")
    public R<List<ConfigAttributeVO>> listByEntityClass(@RequestBody ConfigAttributeDTO configAttributeDTO) {
        return R.success(configAttributeService.listByEntityClass(configAttributeDTO));
    }

}
